package jnBase.view
{
	import jnBase.Data;
	import jnBase.InsSceneTools;
	import jnBase.PSteps;
	import jnBase.ShiyanControl;
	import jnBase.eventdispatcher.JNEventDispatcher;
	import jnBase.script.ScriptSceneTools;
	import jnBase.user.User;
	
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.filters.GlowFilter;
	import laya.maths.Point;
	import laya.media.SoundManager;
	import laya.ui.Button;
	import laya.ui.Image;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Tween;
	
	import ui.tools.wap.SceneToolsWAPUI;
	
	/**
	 * UI操作界面  移动端界面
	 */
	public class SceneToolsWAP extends SceneToolsWAPUI implements InsSceneTools
	{
		
		/**实验步骤显示对象集合*/		
		private var stepControlList:Array=[];
		private static var _ins:SceneToolsWAP;
		private var qiCaiData:Array;
		private var dlg_tools:ToolsBoxWAP;
		private var _script:ScriptSceneTools;

		private var rotate:Boolean=true;
		private var chooseEroor:String="";
		public function SceneToolsWAP()
		{
			super();
			this.mouseThrough=true;
		}
		public static function getInstance():SceneToolsWAP{
			if(_ins==null){
				_ins=new SceneToolsWAP();
			}
			return _ins;
		}
		override public function onAwake():void
		{
			super.onAwake();
			_script=this.addComponent(ScriptSceneTools);
			_script.initData();
			ms=new Image("comp/img_ms1.png");
			ms.mouseThrough=true;
			ms.height=panel.height;
			ms.width=panel.width;
			panel.mask=ms;
			//实验名称
			this.txt_name.text=Data.shiyanData[0].value;
			if(Data.shiyanData[0].value.length>17){
				txt_name.fontSize-=2;
			}
			//背景音乐
			btn_music.on(Event.CLICK,this,playPauseMusic);
			musicPlaying=true;
			SoundManager.playMusic(Data.bgMusic);
			function playPauseMusic():void
			{
				// TODO Auto Generated method stub
				if(musicPlaying){
					pauseMusic();
				}
				else{
					playMusic();
				}
			}
			dlg_tools=new ToolsBoxWAP();
			/**实验目的 */
			btn_mudi.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["mudi"]);
			/**实验原理*/
			btn_yuanli.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["yuanli"]);
			/**实验方法*/
			btn_fangfa.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["fangfa"]);
			/**注意事项*/
			btn_zhuyishixiang.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["zhuyishixiang"]);
			/**实验器材*/
			btn_qicai.on(Event.CLICK,this,shiyanQiCai);
			/**实验试剂*/
			btn_yaopin.on(Event.CLICK,this,shiyanQiCai);
			/**实验PPT课件*/
			btn_ppt.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["ppt"]);
			/**实验结果*/
			btn_jieguo.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["jieguo"]);
			/**实验报告*/
			btn_baogao.on(Event.CLICK,JNEventDispatcher.getInstance(),JNEventDispatcher.getInstance().event,["baogao"]);
			/**器材总集合*/
			qiCaiData=Data.shiyanQiCai.concat(Data.shiyanShiji);
			btn_out.on(Event.CLICK,this,exitShiyan);
			setBtns();
			
			if(Data.isExam  && Data.timeLimit) tip_djs.visible=true;
		}
		
		private function playMusic():void
		{
			// TODO Auto Generated method stub
			SoundManager.playMusic(Data.bgMusic);
			btn_music.skin="mobile/btn_voiceOpen.png";
			musicPlaying=true;
			rotate=true;
		}
		
		public function pauseMusic():void
		{
			// TODO Auto Generated method stub
			SoundManager.stopMusic();
			btn_music.skin="mobile/btn_voiceClose.png";
			musicPlaying=false;
			btn_music.rotation=0;
			rotate=false;
		}
		
		private function exitShiyan():void
		{
			// TODO Auto Generated method stub
			PSteps.exitShiyan();
		}
		
		private function setBtns():void
		{
			// TODO Auto Generated method stub
			//初始化右下角折叠按钮
			controlRotate.pos(1806,546);
			controlRotate.selected = false;
			box_tightDown.rotation = 0;
			controlRotate.skin = "mobile/btn_-.png";
			controlRotate.on(Event.MOUSE_DOWN,this,function():void{
				if(controlRotate.selected)//打开 选中状态
				{
					controlRotate.mouseEnabled = false;
					Tween.to(controlRotate,{y:546},300,null,Handler.create(this,function():void{
						Tween.to(box_tightDown,{rotation:0},300,null,Handler.create(this,function():void{
							controlRotate.mouseEnabled = true;
							controlRotate.selected = false;
							controlRotate.skin = "mobile/btn_-.png";
						}));
					}));
				}
				else
				{
					controlRotate.mouseEnabled = false;
					Tween.to(box_tightDown,{rotation:140},300,null,Handler.create(this,function():void{
						Tween.to(controlRotate,{y:684},300,null,Handler.create(this,function():void{
							controlRotate.mouseEnabled = true;
							controlRotate.selected = true;
							controlRotate.skin = "mobile/btn_+.png";
						}));
					}));
				}	
			});
			
			
			
			//初始化左下方折叠栏
			panel_leftDown.width = 850;
			kejianBox.x = 850;
			panelControl.selected = false;
			panelControl.skin = "mobile/btn_foldHe.png";
			panelControl.on(Event.MOUSE_DOWN,this,function():void{
				if(panelControl.selected)//打开 选中状态
				{
					panelControl.mouseEnabled = false;
					Tween.to(panel_leftDown,{width:850},400);
					Tween.to(kejianBox,{x:850},400,null,Handler.create(this,function():void{
						panelControl.mouseEnabled = true;
						panelControl.selected = false;
						panelControl.skin = "mobile/btn_foldHe.png";
						
					}));
				}
				else
				{
					panelControl.mouseEnabled = false;
					Tween.to(panel_leftDown,{width:175},400);
					Tween.to(kejianBox,{x:175},400,null,Handler.create(this,function():void{
						panelControl.mouseEnabled = true;
						panelControl.selected = true;
						panelControl.skin = "mobile/btn_foldKai.png";
					}));
				}
			});
			
			//初始化右侧提示栏
			Tween.from(box_msg,{x:1566},600,Ease.circInOut);
			tipControl.skin = "mobile/btn_tipHe.png";
			tipControl.on(Event.CLICK,this,onClickTips);
			
			//实验步骤
			btn_shiyanbuzhou.on(Event.CLICK,this,changeStepState);
		}
		private var showtipsBool:Boolean=true;
		private function onClickTips():void
		{
			// TODO Auto Generated method stub
			if(showtipsBool){
				hideTips();
			}
			else {
				Tween.to(box_msg,{x:1566},600,Ease.circInOut);
				tipControl.skin = "mobile/btn_tipHe.png";
			}
			showtipsBool=!showtipsBool;
		}
		/**
		 *实验步骤按钮 
		 * 
		 */		
		private var isShowStep:Boolean=true;
		private function changeStepState():void
		{
			// TODO Auto Generated method stub
			isShowStep?hideStep():showStep();
			isShowStep=!isShowStep;
		}
		/**
		 *显示实验步骤 
		 * 
		 */		
		protected function showStep():void{
			Tween.to(stepBox,{x:0,alpha:1},600,Ease.circInOut);
		}
		/**
		 *隐藏实验步骤 
		 * 
		 */		
		protected function hideStep():void{
			Tween.to(stepBox,{x:-461,alpha:0},300,Ease.circInOut);
		}
		/**
		 *实验器材 
		 * @param e
		 * 
		 */		
		private function shiyanQiCai(e:Event):void{
			if(e.currentTarget==btn_qicai)
			{
				dlg_tools.setData(0);
			}
			else{
				dlg_tools.setData(1);
			}
			dlg_tools.popup();
		}
		
		public function chooseToolsHandler(choosedata:Object):void
		{
			// TODO Auto Generated method stub
			if(Data.rightToolValue.length>0){
				if(choosedata.value==Data.rightToolValue){
					chooseRight();
				}
				else{
					showTips(chooseEroor);
				}
			}
		}
		/**选择工具正确时的回馈 */
		private function chooseRight():void{
			JNEventDispatcher.getInstance().event(JNEventDispatcher.ClOSE_TOOLS);
			JNEventDispatcher.getInstance().event(JNEventDispatcher.CHOOSE_TOOLS_RIGHT);
		}
		public function closeToolsHandler():void
		{
			// TODO Auto Generated method stub
			hideTips();
			Tween.to(box_nextTool,{x:1920,alpha:0},400);
			Data.rightToolValue="";
			box_nextTool.offAll(Event.CLICK);
		}
		
		/**根据提示选择工具
		 * @ $value 工具的唯一键值
		 * @ $tips  选择工具时的提示
		 */
		public function getToolByValue($value:String,$tips:String):void{
			showTips($tips);
			Data.rightToolValue=$value;
			showNextTools();
			chooseEroor=$tips;
			box_nextTool.once(Event.CLICK,this,function():void{
				chooseRight();
			})
		}
		private function showNextTools():void{
			if(Data.rightToolValue.length>0){
				var i:int=0;
				for each (var data:Object in qiCaiData) 
				{
					// trace(data.value,ShiYanData.rightToolValue);
					if(data.value==Data.rightToolValue){
						Tween.to(box_nextTool,{x:1673,alpha:1},400);
						icon.skin=data.img;
						var f:GlowFilter=new GlowFilter("#000000",4,10,10);
						icon.filters=[f];
						icon.scaleY=icon.scaleX=60/icon.width;
						i++;
						break ;
					}
				}
				if(i!=1){
					trace("*****Error:找不到值为"+Data.rightToolValue+"的工具");
				}
				
			}
		}
		/**
		 *设置一级菜单的激活按钮的居中位置 
		 * 
		 */		
		private function setBarPosition():void
		{
			// TODO Auto Generated method stub
			panel.refresh();
			var p:int=Data.currStep-2;
			if(p<0){
				p=0;
			}
			panel.vScrollBar.value=p/Data.stepList.length*Box_tips.height;
		}
		/**
		 *清理定时器并移除子步骤列表
		 * 
		 */		
		private function laterRemove():void
		{
			// TODO Auto Generated method stub
			Laya.timer.clear(this,laterRemove);
			removeSonSteps();
		}
		public  var currStepBtn:StepControlWAP;
		/**
		 *初始化实验步骤  
		 * 
		 */
		private function setStep():void{
			for (var i:int = 0; i < Data.stepList.length; i++) 
			{
				var step:StepControlWAP=new StepControlWAP(i,Data.stepList[i].des,ShiyanControl.hasSonStep(i));
				//考核模式不能点击跳转
				if(!Data.isExam){
					step.on(Event.CLICK,this,choose);
				}
				Box_tips.addChild(step);
				step.x=30;
				stepControlList[i]=step;
				Box_tips.height+=65;
			}
			panel.vScrollBarSkin="";
			panel.vScrollBar.elasticBackTime=300;
			panel.vScrollBar.elasticDistance=200;
			/*panel.vScrollBar.on(Event.CHANGE,this,function():void{
				trace("setBarPositionbar*****************:"+panel.vScrollBar.value+"max:"+panel.vScrollBar.max);
			});*/
			laterRemove();
			Laya.timer.once(300,this,addSonStep);
			//选择步骤
			//如果有子步骤，点击一次打开子步骤列表，再点击一次关闭子步骤列表
			function choose(e:Event):void{
				var s:StepControlWAP=e.currentTarget as StepControlWAP;
				if(e.target!=s.btn){
					return ;
				}
				isNatural=false;
				if(currStepBtn!=s){
					if(currStepBtn){
						currStepBtn.select=false;
					}
					s.select=true;
					Data.currStep=s.value;
					Data.currSonStep=0;
					currSonStepBtn=null;
					//手动切换步骤事件
					JNEventDispatcher.getInstance().event(JNEventDispatcher.CLICK_ADD_PAGE);
					laterRemove();
					if(currStepBtn.height>65){
						Box_tips.height-=currStepBtn.height;
						currStepBtn.height=65;
					}
					currStepBtn.setSkin(currStepBtn.hasSon);
					currStepBtn=s;
					currStepBtn.setSkin(false);
					
				}
				else{
					if(isShowSonstep){
						currStepBtn.setSkin(currStepBtn.hasSon);
						laterRemove();
						return ;
					}
					else{
						currStepBtn.setSkin(false);
					}
				}
				//增加二级子步骤
				addSonStep();
			}
		}
		private var line:Sprite=new Sprite();
		public var currSonStepBtn:TwoStepControl;
		private var isShowSonstep:Boolean=false;
		private var sonStepList:Array=[];
		private var ms:Image;
		/**
		 *添加子步骤列表 
		 * 
		 */		
		private function addSonStep():void
		{
//			laterRemove();
			if(Data.stepList[Data.currStep].value.length>0){
				//				trace("增加二级按钮："+Data.stepList[Data.currStep].value);
				isShowSonstep=true;
				for (var i:int = 0; i < Data.stepList[Data.currStep].value.length; i++) 
				{
					var btn:TwoStepControl=new TwoStepControl(Data.stepList[Data.currStep].value[i],i);
					var global:Point=currStepBtn.localToGlobal(new Point(0,0));
					panel.globalToLocal(global);
					line.graphics.clear();
					currStepBtn.twoParent.addChild(btn);
					currStepBtn.height+=(54);
					//考核模式不能点击跳转
					if(!Data.isExam){
						btn.on(Event.CLICK,this,choose);
					}
					sonStepList[i]=btn;
				}
				currStepBtn.height+=10;
				Box_tips.height+=currStepBtn.height;
			}
			if(isNatural){
				Laya.timer.callLater(this,setBarPosition);
			}
			//选择二级步骤
			function choose(e:Event):void{
				var tg:TwoStepControl=e.currentTarget as TwoStepControl;
				if(tg!=currSonStepBtn){
					if(currSonStepBtn) currSonStepBtn.titleName.selected=false;
					currSonStepBtn=tg;
					currSonStepBtn.titleName.selected=true;
					Data.currSonStep=Number(currSonStepBtn.name);
					isNatural=false;
					//手动切换二级步骤事件
					JNEventDispatcher.getInstance().event(JNEventDispatcher.CLICK_SON_STEPS);
				}
			}
		}
		/**
		 *移除二级按钮 
		 * 
		 */		
		private function removeSonSteps():void{
			trace("removeSonSteps");
			isShowSonstep=false;
			for each (var btn:Button in sonStepList) 
			{
				btn.offAll(Event.CLICK);
				btn.removeSelf();
				line.graphics.clear();
				line.removeSelf();
			}
			if(currSonStepBtn) {
				if(currStepBtn.height>65){
					Box_tips.height-=currStepBtn.height;
					currStepBtn.height=65;
				}
			}
			
		}
		public function showSonStepByIndex($ind:int):void
		{
			// TODO Auto Generated method stub
			for each (var btn:TwoStepControl in sonStepList) 
			{
				if(btn.name==$ind+""){
					btn.titleName.selected=true;
					currSonStepBtn=btn;
					Data.currSonStep=$ind;
					JNEventDispatcher.getInstance().event(JNEventDispatcher.NATURAL_SON_STEPS);
				}
				else{
					btn.titleName.selected=false;
				}
			}
		}
		/**
		 *步骤完成后跳转时自动设置滚动条的位置
		 */		
		private var isNatural:Boolean=false;

		private var musicPlaying:Boolean;
		public function showStepByIndex($currStep:int):void
		{
			// TODO Auto Generated method stub
			Data.currStep=$currStep;
			laterRemove();
			isNatural=true;
			setBarPosition();
			for each (var step:StepControlWAP in stepControlList) 
			{
				if(step.value==$currStep){
					step.select=true;
					currStepBtn=step;
					Data.currSonStep=0;
					JNEventDispatcher.getInstance().event(JNEventDispatcher.NATURAL_ADD_PAGE);
					addSonStep();
				}
				else{
					step.select=false;
				}
			}
		}
		
		public function showStepByValue($steps:String):void
		{
			// TODO Auto Generated method stub
			var temp:Array=$steps.split("*");
			var ind:int=parseInt(temp[0])-1;
			if(ind>=Data.stepList.length || ind<0){
				ind=0;
			}
			Data.currStep=ind;
			var sonstep:int=parseInt(temp[1])-1;
			if(Data.stepList[Data.currStep].value.length>0){//有二级步骤
				if(sonstep<0) {
					sonstep=0;
				}
				if(sonstep>Data.stepList[Data.currStep].value.length-1){
					sonstep=Data.stepList[Data.currStep].value.length-1;
				}
				Data.currSonStep=sonstep;
			}
			isNatural=true;
			setStep();
		}
		
		/**
		 *提示操作步骤 
		 * @param str 提示信息
		 * @param bAutoHide  是否自动隐藏
		 * @param interval   如果自动隐藏，停留时间
		 * 
		 */		
		public function showTips(str:String,bAutoHide:Boolean=false,interval:int=5000):void{
			//有时配音后面需要跟一些动态数字，所以只读*前面的汉字，后面的数字不读出
			var temp:Array=str.split("*");
			if(Data.currSoundUrl && Data.currSoundUrl.length>0) SoundManager.stopSound(Data.currSoundUrl);
			Data.currSoundUrl="sound/peiyin/"+temp[0]+".mp3";
			SoundManager.playSound(Data.currSoundUrl,1);
			if(temp.length>1){
				lab_msg.text=temp[0]+temp[1];	
			}
			else lab_msg.text=temp[0];
			Tween.to(box_msg,{x:1566},600,Ease.circInOut);
			tipControl.skin = "mobile/btn_tipHe.png";
			if(bAutoHide) Laya.timer.once(interval,this,hideTips);
		}
		/**
		 *隐藏操作提示信息 
		 * 
		 */		
		public function hideTips():void{
			Tween.to(box_msg,{x:1848},600,Ease.circInOut);
			tipControl.skin = "mobile/btn_tipKai.png";
		}
		public function update():void
		{
			// TODO Auto Generated method stub
			if(rotate){
				btn_music.rotation+=1.5;
			}
		}
		/**
		 *更新时间 
		 * 
		 */		
		public function updateTime($time:String,$etime:String):void
		{
			// TODO Auto Generated method stub
			txt_time.text=$time;
			txt_djs.text=$etime;
		}
		public function getUserInfo(data:Object):void
		{
			// TODO Auto Generated method stub
			Data.CurrUser=new User(data.Body);
			userPhoto.skin=(Data.CurrUser.HeadIcon);
			userName.text=Data.CurrUser.RealName;
			jifenNum.text='';
			jingyanNum.text='';
		}
		
	}
}